package IO;

import java.io.*;
//读取文件
public class Demo7 {
    public static void main(String[] args) {
        try(InputStream inputStream = new FileInputStream("./text.txt")) {
            while (true) {
                byte[] bytes = new byte[1024];
                int len = inputStream.read(bytes);
                //最多读取 b.length 字节的数据到 bytes 中，返回实际读到的数量；-1 代表明读取完了
                if(len == -1) {  //（0-255）出现-1表明读取完了
                    break;
                }
                for (int i = 0; i < len; i++) {
                    System.out.println(bytes[i]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main2(String[] args) {   //一个一个读
        try(InputStream inputStream = new FileInputStream("./text.txt")) { //try里面的会自动关闭文件，但必须实现Closeable
            while (true) {
                int a = inputStream.read();
                if(a == -1) {
                    break;
                }
                System.out.println(a);
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main1(String[] args) throws IOException {   //一个一个读
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("./text.txt");
            while (true) {
                int a = inputStream.read();
                if(a == -1) {
                    break;
                }
                System.out.println(a);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            inputStream.close();
        }
    }
}
